package com.usdt.system.service.impl.cp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.usdt.common.core.domain.entity.SysDictData;
import com.usdt.common.core.domain.entity.SysUser;
import com.usdt.system.cp.utils.UserAndDeptUtils;
import com.usdt.system.domain.req.BatchTransferOutReqVO;
import com.usdt.system.domain.resp.BatchClueRespVO;
import com.usdt.system.domain.resp.BatchVisitRespVO;
import com.usdt.system.domain.resp.SalespersonRespVO;
import com.usdt.system.service.ISysDictTypeService;
import com.usdt.system.service.ISysUserService;
import com.usdt.system.service.cp.BatchManagementService;
import com.usdt.system.service.cp.ClueRegisterService;
import com.usdt.system.service.cp.VisitRegisterService;

@Service
public class BatchManagementServiceImpl implements BatchManagementService{
	
	private static final Integer Sales_Person_Id = 101;
	
	@Value("${sys.properties.ky.main_dept_id}")
	public String mainDeptId;
	
	@Autowired
	private ClueRegisterService clueRegisterService; 
	
	@Autowired
	private VisitRegisterService visitRegisterService; 
	
    @Autowired
    private ISysUserService userService;
    
    @Autowired
    private ISysDictTypeService dictTypeService;

	@Override
	public List<BatchClueRespVO> getBatchClueByPhoneNumber(String phoneNumber) {
		//查询部门
		List<String> deptIds = this.getDeptIds();
		
		return clueRegisterService.getBatchClueByPhoneNumber(phoneNumber,deptIds);
	}
	
	/** 获取部门权限
	 * @return
	 */
	private List<String> getDeptIds() {
		String cpDeptIds = null;
		String currentDeptId = UserAndDeptUtils.getCurrentDeptId();
		String currentUserId = UserAndDeptUtils.getCurrentUserId();
		if(mainDeptId.equals(currentDeptId)) {
			cpDeptIds=null;
		}else {
			//查询人员部门
			SysUser sysUser = userService.selectUserByCpId(currentUserId);
			cpDeptIds = sysUser.getCpDeptId();
		}
		return StringUtils.isBlank(cpDeptIds)?null:Arrays.asList(cpDeptIds.split(","));
	}

	@Override
	public List<BatchVisitRespVO> getVisitByPhoneNumber(String phoneNumber) {
		//查询部门
		List<String> deptIds = this.getDeptIds();
		return visitRegisterService.getVisitByPhoneNumber(phoneNumber,deptIds);
	}

	@Override
	public List<SalespersonRespVO> salesperson() {
		List<SalespersonRespVO> result = new ArrayList<SalespersonRespVO>();
		SysUser currentUser = userService.selectUserByCpId(UserAndDeptUtils.getCurrentUserId());
		List<String> currentUserDeptIds = Arrays.asList(currentUser.getCpDeptId().split(","));
		
		List<SysUser> userList = userService.getUserListByRoleId(Sales_Person_Id);
		List<SysDictData> projectData = dictTypeService.selectDictDataByType("project_manage");
		if(!CollectionUtils.isEmpty(userList)) {
			for (SysUser sysUser : userList) {
				List<String> asList = Arrays.asList(sysUser.getCpDeptId().split(","));
				for (String deptId : asList) {
					if(currentUserDeptIds.contains(deptId)) {
						SalespersonRespVO respVO = new SalespersonRespVO();
						SysDictData project = projectData.stream().filter(e->deptId.equals(e.getDictValue())).findAny().orElse(null);
						respVO.setUserId(sysUser.getCpUserid());
						respVO.setUserName(sysUser.getNickName());
						respVO.setDeptId(deptId);
						respVO.setDeptName(project.getDictLabel());
						result.add(respVO);
					}
				}
			}
		}
		return result;
	}

	@Override
	public Boolean clueBatchTransferOut(BatchTransferOutReqVO vo) {
		
		return clueRegisterService.batchTransferOut(vo);
	}

	@Override
	public Boolean visitBatchTransferOut(BatchTransferOutReqVO vo) {
		
		return visitRegisterService.batchTransferOut(vo);
	}

	@Override
	public IPage<BatchClueRespVO> batchClueTransferOutPage(String source, String userId, String deptId,String phoneNumber, Integer pageNum,
			Integer pageSize) {
		
		List<String> deptIds = new ArrayList<>();
		if(StringUtils.isBlank(deptId)) {
			deptIds = this.getDeptIds();
			//deptId = UserAndDeptUtils.getCurrentDeptId();
		}else {
			deptIds.add(deptId);
		}
		List<String> sourceList = new ArrayList<String>();
		if(StringUtils.isNotBlank(source)) {
			List<String> asList = Arrays.asList(source.split(","));
			if(!asList.contains("0")) {
				sourceList.addAll(asList);
			}
		}
		return clueRegisterService.batchClueTransferOutPage(sourceList,userId,deptIds,phoneNumber,pageNum,pageSize);
	}

	@Override
	public IPage<BatchVisitRespVO> batchVisitTransferOutPage(String source, String userId, String deptId,String phoneNumber,
			Integer pageNum, Integer pageSize) {
		
		List<String> deptIds = new ArrayList<>();
		if(StringUtils.isBlank(deptId)) {
			deptIds = this.getDeptIds();
			//deptId = UserAndDeptUtils.getCurrentDeptId();
		}else {
			deptIds.add(deptId);
		}
		List<String> sourceList = new ArrayList<String>();
		if(StringUtils.isNotBlank(source)) {
			List<String> asList = Arrays.asList(source.split(","));
			if(!asList.contains("0")) {
				sourceList.addAll(asList);
			}
		}
		return visitRegisterService.batchVisitTransferOutPage(sourceList,userId,deptIds,phoneNumber,pageNum,pageSize);
	}

}
